package wcm;

import wcm.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import static org.junit.Assert.assertTrue;

/**
 * Checks Bato is near a corner too long. Every turn, the position of Bato
 * is checked if it is at a corner.
 * @author wing
 *
 */
public class TestBato4 extends RobotTestBed {
  
  int cornerEncountered = 0;
  int cornerDuration = 0;
  
  /**
   * Specifies that SittingDuck and Bato are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,wcm.Bato";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /** 
   * Checks if the Bato is at a corner.
   * @param event Details about the completed battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    
    IRobotSnapshot snapshot = event.getTurnSnapshot().getRobots()[1];
    double posX = snapshot.getX();
    double posY = snapshot.getY();
    int topLeft = 1;
    int topRight = 2;
    int bottomRight = 3;
    int bottomLeft = 4;
    
    //check all four corners
    if ((posX > (800 - 20)) && (posY > (600 - 20))) {
      
      if (cornerEncountered == topRight) {
        cornerDuration ++;
      }
      
      else {
      cornerDuration = 0;
      cornerEncountered = topRight;
      
      }
    }
    
    else if ((posX > (800 - 20)) && (posY < 20)) {
      
      if (cornerEncountered == bottomRight) {
        cornerDuration ++;
      }
      
      else {
      cornerDuration = 0;
      cornerEncountered = bottomRight;
      
      }
      
    }
    
    else if (( posY > (600 - 20)) && (posX <  20)) {
      
      if (cornerEncountered == topLeft) {
        cornerDuration ++;
      }
      
      else {
      cornerDuration = 0;
      cornerEncountered = topLeft;
      
      }
      
    }   
    
    else if (( posY < 20) && ( posX < 20)) {
      if (cornerEncountered == bottomLeft) {
        cornerDuration ++;
      }
      
      else {
      cornerDuration = 0;
      cornerEncountered = bottomLeft;
      
      }
      
    }

  }
  
  
  /** 
   * This method checks whether Bato has been at the corner too many times.
   * Which means it's been stuck there.
   * @param event Details about the completed battle.
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    
    Boolean stuckCorner = false;
    
    if ((cornerDuration) > 5) {
      stuckCorner = true;
    }
    
    //reset for next round
    cornerEncountered = 0;
    cornerDuration = 0;
    
    assertTrue("Retaliation", !stuckCorner);
    
  }
  
  
}
